package com.gis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gis.dto.BiologyReportDTO;
import com.gis.entity.BiologyData;
import com.gis.entity.WaterQualityData;
import com.gis.mapper.BiologyDataMapper;
import com.gis.maps.commonMap;
import com.gis.service.IBiologyDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import static org.apache.commons.lang.StringUtils.capitalize;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bobo
 * @since 2024-11-28
 */
@Service
public class BiologyDataServiceImpl extends ServiceImpl<BiologyDataMapper, BiologyData> implements IBiologyDataService {

    @Autowired
    private BiologyDataMapper biologyDataMapper;
    @Override
    public List<BiologyReportDTO> getBiologyQualityAvgByYearAndSeason(int year, String season) {
        return biologyDataMapper.getBiologyQualityAvgByYearAndSeason(year, season);
    }

    @Override
    public List<Float> getLastN(String param, int n, int id) {
        List<Float> data = new ArrayList<>();
        List<BiologyData> biologyData = biologyDataMapper.getLastN(param, n, id);
        for(BiologyData b : biologyData){
            String methodName = "get" + capitalize(commonMap.getParam(param));
            try {
                Method method = WaterQualityData.class.getMethod(methodName);
                Float result = (Float) method.invoke(b);
                data.add(result);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        return data;
    }
}
